<template>
    <div class="common-page page-corp-demands" v-loading.fullscreen="loading">
        <div class="common-page__head">
            <!-- <comp-paths class="route" :paths="['/corp', $route.fullPath]"></comp-paths> -->
            <div class="title">{{ $options.title }}</div>
        </div>
        <comp-demands class="common-page__body" ref="list" select link>
            <template #head="scope">
                <el-button type="plain" size="small" icon="el-icon-refresh" @click="search()">刷新列表</el-button>
                <el-button type="danger" size="small" icon="el-icon-delete" @click="delDemand(...scope.selects)">删除需求</el-button>
            </template>
            <template #body="scope">
                <el-button type="primary" size="mini" @click.stop="setDemand('start', scope.data)">审核</el-button>
                <el-button type="danger" size="mini" @click.stop="delDemand(scope.data)">删除</el-button>
            </template>
        </comp-demands>
        <comp-dialog title="审核需求" width="400px" ref="demand">
            <el-form size="small" label-width="98px" @submit.native.prevent v-if="demand">
                <el-form-item label="需求标题：">
                    <div style="margin: 7px 20px 7px 0; line-height: 18px">{{ demand.title }}</div>
                </el-form-item>
                <el-form-item label="需求内容：">
                    <div style="margin: 7px 20px 7px 0; line-height: 18px">{{ demand.content }}</div>
                </el-form-item>
                <el-form-item label="需求附件：">
                    <div style="margin: 7px 20px 7px 0">
                        <comp-files-viewer :files="demand.files" v-if="demand.files.length > 0"></comp-files-viewer>
                    </div>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-check" @click="setDemand('submit', 'yes')">通过</el-button>
                    <el-button type="danger" icon="el-icon-check" @click="setDemand('submit', 'not')">不通过</el-button>
                    <el-button type="plain" icon="el-icon-close" @click="setDemand('end')">放弃</el-button>
                </el-form-item>
            </el-form>
        </comp-dialog>
    </div>
</template>

<script>
    import mixin from "../mixin";
    import { string, int, deepCopy } from "../util";
    export default {
        name: "page-corp-demands",
        title: "企业/项目需求管理",
        nodes: ["/menu/corp"],
        childs: [],
        mixins: [mixin],
        data: () => ({
            loading: false,
            demand: null,
        }),
        methods: {
            search() {
                if (this.loading) return;
                this.$nextTick(() => this.$refs.list.init());
            },
            setDemand(action, data) {
                if (this.loading) return;
                let form = this.demand;
                if (!action || action === "start") {
                    this.demand = deepCopy(data);
                    this.$refs.demand.init();
                } else if (action === "submit") {
                    form.approved = data;
                    this.loading = true;
                    this.plugins.user.setDemand(form).then((res) => {
                        this.loading = false;
                        if (res.code === 0) {
                            this.plugins.modal.toast("操作成功！", "success");
                            this.setDemand("end");
                            this.search();
                        } else {
                            this.plugins.modal.alert("操作失败", res.code + "：" + res.msg, "error");
                        }
                    });
                } else if (action === "end") {
                    this.$refs.demand.close();
                }
            },
            delDemand(...demands) {
                if (this.loading) return;
                if (demands.length === 0) return this.plugins.modal.toast("请先勾选要删除的需求哦！", "warning");
                let content = " <span style='color:red;'>" + (demands.length > 1 ? "勾选的" + demands.length + "条需求" : demands[0].title) + "</span> 吗？";
                this.plugins.modal.alert("删除确认", "确定删除" + content, "warning", ["确定删除", "再想想"]).then((ok) => {
                    if (!ok) return;
                    this.loading = true;
                    this.plugins.user.delDemand(...demands).then((res) => {
                        this.loading = false;
                        if (res.code === 0) {
                            this.plugins.modal.toast("删除成功！", "success");
                            this.search();
                        } else {
                            this.plugins.modal.alert("删除失败", res.code + "：" + res.msg, "error");
                        }
                    });
                });
            },
            destroy() {
                if (this.loading) return;
                return true;
            },
            init() {
                if (this.loading) return;
                this.loading = true;
                this.$root.$refs.navbar.init();
                this.plugins.user.call(async () => {
                    let res = await this.plugins.user.init();
                    if (res.code === 0) { }
                    return res;
                }, async () => {
                    if (this.access(this.$route.name)) return { code: 0 };
                    return this.env.xhrErrors.find((error) => error.code === 4);
                }).then((res) => {
                    this.loading = false;
                    this.$root.$refs.navbar.init();
                    if (res.code === 0) {
                        this.search();
                    } else if (res.code === 2) {
                        this.$root.$refs.navbar.login();
                    } else {
                        this.plugins.modal.alert("页面加载失败", res.code + "：" + res.msg, "error").then(() => this.$router.replace("/home"));
                    }
                });
            },
        },
    };
</script>

<style lang="scss">
    .page-corp-demands {
        overflow: hidden;
    }
</style>